﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Student
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        //连接字符串
        public static string constr = "Server=.;user Id=sa;pwd=123456;database=Mxdx;";
        //加载驱动
        public static SqlConnection sqlcon = new SqlConnection(constr);
        //程序加载时打开数据库
        private void Form1_Load(object sender, EventArgs e)
        {
            //判断驱动连接状态如果是关闭的，就打开
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
        }
        //皮肤控件
        private void cmb_Style_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmb_Style.Text=="金属黑")
            {
                this.skinEngine1.Active = true;//启动皮肤控件
                this.skinEngine1.SkinFile = "skin/CalmnessColor1.ssk";//皮肤文件
            }
            else if(this.cmb_Style.Text=="宝石蓝")
            {
                this.skinEngine1.Active = true;
                this.skinEngine1.SkinFile = "skin/EmeraldColor1.ssk";
            }
            else
            {
                this.skinEngine1.Active = false;
            }
        }

        //定义管理员账号密码
        string userName = "HYR";
        string pwd = "123456";
        //登录
        private void btn_Login_Click(object sender, EventArgs e)
        {
            //获取用户输入的账号
            string userName = this.txt_UserName.Text.Trim();
            //获取用户输入的密码
            string pwd = this.txt_Pwd.Text.Trim();
            //如果账号密码都匹配
            if (userName == this.userName && pwd == this.pwd)
            {
                //实例化学生管理中心窗体
                StuInfoManagementSystem stu = new StuInfoManagementSystem();
                //打开学生管理中心窗体
                stu.Show();
                //学生管理中心窗体显示在最前面
                stu.BringToFront();
                //隐藏登录窗体
                this.Hide();
            }
            //当用户名、密码不正确或为空时
            else
            {
                var uName = string.IsNullOrEmpty(userName);
                var uPwd = string.IsNullOrEmpty(pwd);
                if (uName && uPwd)
                {
                    MessageBox.Show("用户名和密码不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (uName)
                {
                    MessageBox.Show("用户名不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (uPwd)
                {
                    MessageBox.Show("密码不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("用户名或密码不正确！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        //在输入完密码之后敲击回车键即可实现登录
        private void txt_Pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Login_Click(sender,e);
            }
        }
        //在用户名处敲击回车键实现登录效果
        private void txt_UserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Login_Click(sender,e);
            }
        }
        //关闭系统
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
